import Ext from '@/common/web_extension'
const local = Ext.storage.local

const extensionStorageLocal = {
  get: async (key: string): Promise<any> => {
    return await local.get(key).then((obj: any) => obj[key])
  },

  set: async (key: string, value: any): Promise<boolean> => {
    return await local.set({ [key]: value }).then(() => true)
  },

  remove: async (key: string): Promise<boolean> => {
    return await local.remove(key).then(() => true)
  },

  clear: async (): Promise<boolean> => {
    return await local.clear().then(() => true)
  },

  addListener: (fn: Function): void => {
    Ext.storage.onChanged.addListener((changes: any) => {
      const list = Object.keys(changes).map(key => ({ ...changes[key], key }))
      fn(list)
    })
  }
}

export default extensionStorageLocal